<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.equipmentTypeId"
        placeholder="设备类型"
        @change="selectEquipmentType(listQuery.equipmentTypeId)"
      >
        <el-option
          v-for="item in equipmentTypeList"
          :key="item.id"
          :label="item.typeName"
          :value="item.id"
          :disabled="item.disabled"
        >
        </el-option>
      </el-select>

      <el-select
        v-model="listQuery.equipmentProductId"
        placeholder="设备名称"
        @change="selectEquipmentProduct(listQuery.equipmentProductId)"
      >
        <el-option
          v-for="item in equipmentProductList"
          :key="item.id"
          :label="item.typeName"
          :value="item.id"
          :disabled="item.disabled"
        >
        </el-option>
      </el-select>

      <!-- {{listQuery.equipmentModelId}} -->
      <el-select
        v-model="listQuery.equipmentModelId"
        placeholder="设备型号"
        @change="selectEquipmentModel(listQuery.equipmentModelId)"
      >
        <el-option
          v-for="item in equipmentModelList"
          :key="item.id"
          :label="item.typeName"
          :value="item.id"
          :disabled="item.disabled"
        >
        </el-option>
      </el-select>

      <DictSelect
        v-model="listQuery.equipmentUseStatus"
        placeholder="设备使用情况"
        type="equipment-use-status"
      />

      <el-input
        v-model="listQuery.macAddress"
        class="filter-item"
        clearable
        placeholder="MAC地址"
        style="width: 150px"
        @keyup.enter.native="handleFilter"
      />

      <el-button
        v-waves
        v-permission="['sys:role:list']"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >搜索</el-button
      >
      <el-button
        v-waves
        style="margin-right: 153px; float: right"
        type="primary"
        icon="el-icon-plus"
        @click="addEquipment()"
        >录入设备</el-button
      >
    </div>

    <el-table
      :key="tableKey"
      :data="list"
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="设备id" prop="id" width="70px" align="center" />
      <!-- <el-table-column label="注册时间" prop="registeredAt" align="center" /> -->
      <el-table-column label="设备类型" prop="type" align="center" />
      <el-table-column label="设备名称" prop="product" align="center" />
      <el-table-column label="设备型号" prop="modle" align="center" />
      <el-table-column label="MAC地址" prop="macAddress" align="center" />
      <el-table-column
        label="使用情况"
        prop="equipmentUseStatus"
        :formatter="getEquipmentUseStatusName"
        align="center"
      />
      <el-table-column label="操作" align="center" width="500px">
        <template slot-scope="{ row }">
          <el-button
            v-waves
            v-permission="['sys:role:save']"
            style="margin-left: 10px"
            type="primary"
            @click="discard(row.id)"
            >废弃</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="getEquipmentList"
    />

    <!-- 以下是录入设备的弹窗 -->
    <el-dialog
      title="设备录入"
      :visible.sync="dialogAddEquipment"
      width="600px"
    >
      <el-form ref="dataForm" :rules="rules" :model="equipment">
        <el-form-item
          label="设备类型"
          :label-width="formLabelWidth"
          prop="equipmentTypeIdDialog"
        >
          <el-select
            v-model="equipment.equipmentTypeIdDialog"
            placeholder="设备类型"
            @change="selectEquipmentTypeDialog(equipment.equipmentTypeIdDialog)"
          >
            <el-option
              v-for="item in equipmentTypeList"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="设备名称"
          :label-width="formLabelWidth"
          prop="equipmentProductDialog"
        >
          <el-select
            v-model="equipment.equipmentProductDialog"
            placeholder="设备名称"
            @change="
              selectEquipmentProductDialog(equipment.equipmentProductDialog)
            "
          >
            <el-option
              v-for="item in equipmentProductListDialog"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </el-form-item>
    

        <el-form-item
          label="设备型号"
          :label-width="formLabelWidth"
          prop="equipmentModelDialog"
        >
          <el-select
            v-model="equipment.equipmentModelDialog"
            placeholder="设备型号"
          >
            <!-- @change="selectEquipmentModelDialog(equipment.equipmentModelDialog)" -->

            <el-option
              v-for="item in equipmentModelListDialog"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item
          label="设备型号"
          :label-width="formLabelWidth"
          prop="equipmentModelDialog"
        >
          <el-select
            v-model="equipment.equipmentModelDialog"
            placeholder="设备型号"
          >
          
            <el-option
              v-for="item in equipmentModelListDialog"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </el-form-item> -->

        <el-form-item
          label="MAC地址"
          :label-width="formLabelWidth"
          prop="macAddress"
        >
          <el-input
            style="width: 193px"
            v-model="equipment.macAddress"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>

      <div style="text-align: center" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogAddEquipmentSubmit()"
          >确 定</el-button
        >
        <el-button @click="dialogAddEquipment = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 以上是录入设备的弹窗 -->
  </div>
</template>

<script>
import {
  getEquipmentList, //获取设备类型的list
  getEquipmentTypeList,
  getProductInfoList,
  createEquipment,
  discard,
} from "@/api/equipment/equipment.js";

import DatePicker from "@/components/DatePickerUser";
import waves from "@/directive/waves"; // Waves directive
import DictSelect from "@/components/DictSelect";
import LineChartVue from "../dashboard/admin/components/LineChart.vue";
export default {
  name: "EquipmentTable",
  directives: { waves },
  filters: {},
  components: {
    DictSelect,
  },

  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        orderByField: "createdAt",
        orderBy: "desc",

        //选择的设备类型id
        equipmentTypeId: "",

        //选择的设备名称id
        equipmentProductId: "",
        equipmentModelId: "",
        macAddress: "",
      },

      //设备类型  集合
      equipmentTypeList: [],
      //设备产品  集合
      equipmentProductList: [],
      //设备型号  集合
      equipmentModelList: [],

      //对话弹窗里面的设备类型list
      // equipmentTypeListDialog :[],

      //录入设备的弹窗
      dialogAddEquipment: false,
      formLabelWidth: "120px",

      equipmentProductListDialog: [],

      equipmentModelListDialog: [],

      //添加的新的对象
      equipment: {
        equipmentTypeIdDialog: "",
        equipmentProductDialog: "",
        equipmentModelDialog: "",
        macAddress: "",
      },

      //关于规则的
      rules: {
        equipmentTypeIdDialog: [
          { required: true, message: "请选择设备类型", trigger: "blur" },
        ],
        equipmentProductDialog: [
          { required: true, message: "请选择设备名称", trigger: "blur" },
        ],
        equipmentModelDialog: [
          { required: true, message: "请选择设备型号", trigger: "blur" },
        ],
        macAddress: [
          { required: true, message: "mac地址不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    // 加载信息 获取设备类型
    this.getEquipmentTypeList();
    this.getEquipmentList();
  },
  methods: {
    //获取设备类型 ,不分页
    getEquipmentTypeList() {
      getEquipmentTypeList().then((response) => {
        //清空下级的数据
        this.equipmentTypeList = response;
        // this.equipmentTypeListDialog =response  ;
      });
    },

    //选择设备类型
    selectEquipmentType(id) {
      //根据父id 把子级选出来
      getProductInfoList(id).then((response) => {
        //清空下级的数据
        this.equipmentProductList = null;
        this.listQuery.equipmentProductId = "";
        this.equipmentModelList = null;
        this.listQuery.equipmentModelId = "";

        this.equipmentProductList = response;
      });
    },
    //选择设备产品  。。。显示型号
    selectEquipmentProduct(id) {
      //根据父id 把子级选出来
      getProductInfoList(id).then((response) => {
        this.equipmentModelList = null;
        this.listQuery.equipmentModelId = "";

        this.equipmentModelList = response;
      });
    },
    selectEquipmentModel(id) {
      //根据父id 把子级选出来
    },

    //添加设备的按钮
    addEquipment() {
      //打开录入设备的按钮,,,,
      this.dialogAddEquipment = true;
      //清空数据
  
      this.equipment.equipmentTypeIdDialog = "";
      this.equipment.equipmentProductDialog = "";
      this.equipment.equipmentModelDialog = "";
      this.equipment.macAddress = "";

      //清空规则
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },

    //选择类型 的对话框 change 事件
    selectEquipmentTypeDialog(id) {
      //2 ,发送ajax 查询后面的数据并且展示
      getProductInfoList(id).then((response) => {
        //1,清空后面的数据
        this.equipmentProductListDialog = null;
        this.equipment.equipmentProductDialog = "";

        this.equipmentModelListDialog = null;
        this.equipment.equipmentModelDialog = "";

        this.equipmentProductListDialog = response;
      });
    },

    //选择产品 的对话框 change事件
    selectEquipmentProductDialog(id) {
      //2 发送ajax 查询后面的数据并且展示
      getProductInfoList(id).then((response) => {
        //1 清空后面的数据
        //this.equipmentModelListDialog = null;
        this.equipment.equipmentModelDialog = "";

        this.equipmentModelListDialog = response;
      });
    },

    //选择型号的
    selectEquipmentModelDialog() {},

    //添加新数据的窗口 的确定按钮
    dialogAddEquipmentSubmit() {
      //发送ajax 添加数据

      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          createEquipment(this.equipment).then(() => {
            // 添加完成 查询数据
            this.getEquipmentList();
            //清空数据,并且变为false
            this.dialogAddEquipment = false;
          });
        }
      });

      //createEquipment();
    },

    // 获取数据
    getEquipmentList() {
      //    console.log(this.listQuery);
      getEquipmentList(this.listQuery).then((response) => {
        this.list = response.list;
        this.total = response.totalCount;
      });
    },

    //废弃按钮
    discard(id) {
      this.$confirm("此操作将废弃此设备, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        discard(id).then(() => {
          this.$notify({
            title: "成功",
            message: "废弃成功",
            type: "success",
            duration: 2000,
          });
          this.getEquipmentList();
        });
      });

      //alert(id);
      // discard(id).then(() => {
      //   this.getEquipmentList();
      // });
    },

    // 使用情况的name
    getEquipmentUseStatusName(row, column) {
      const status = row[column.property];
      if (status != null) {
        //1: 未使用 2:使用中 3:已废弃 4:已停用
        if (status == 1) {
          return "未使用";
        } else if (status == 2) {
          return "使用中";
        } else if (status == 3) {
          return "已废弃";
        } else if (status == 4) {
          return "已停用";
        }
      } else {
        return "";
      }
    },
    handleFilter() {
      this.listQuery.pageNo = 1;
      this.getEquipmentList();
    },
  },
};
</script>
